﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using CoreShop.Model;
using CoreShop.Services;
using Sunny.UI;
using CoreShop.Library;
using System.Linq;
using SqlSugar.Extensions;
using System.Diagnostics.SymbolStore;
using System.Security.Cryptography;
using NPOI.SS.Formula.Functions;
using SqlSugar;

namespace CoreShop.App
{
    public partial class frmEmployeeSelect : UIForm
    {
        public List<corecmsemployee> employeeList = new List<corecmsemployee>();
        public corecmsemployee selectModel = null;
        public bool _setDisabled;
        public int selectId = 0;

        public frmEmployeeSelect(int _selectId)
        {
            selectId = _selectId;
            InitializeComponent();
        }

        private void frmTableOpen_Load(object sender, EventArgs e)
        {
            BindEmpList();
            numKeybord.FillColor = System.Drawing.Color.White;
            txtValue.TextBox.GotFocus += txtValue_GotFocus;            
        }

        private void txtValue_GotFocus(object sender, System.EventArgs e)
        {
            numKeybord.SetTextBox((UIEdit)sender, UITextBox.UIEditType.Double, 2);
        }

        /// <summary>
        /// 商品按钮名称
        /// </summary>
        /// <param name="deskId"></param>
        /// <returns></returns>
        private string GetFoodItemName(int index)
        {
            return "uiFoodItem_" + index.ToString();
        }

        private void BindEmpList()
        {
            employeeList = new corecmsemployeeServices().AsQueryable().Where(p => p.isDelete == false).ToList();
            fly_datalist.Visible = false;
            this.fly_datalist.SuspendLayout();// 添加很多时挂起会更快（不触发界面更改重新绘图事件）
            if (selectId == 0)
                selectModel = employeeList.Find(p => p.isDefault == 0);

            foreach (var employee in employeeList)
            {
                if (selectId == employee.id) 
                    selectModel = employee;
                else 
                if (selectModel == null) 
                    selectModel = employee;       
                
                DishItemInfo dish = new DishItemInfo();
                dish.Name = employee.name.ToString();
                dish.Money = employee.mobile;                
                var item = new UIDishItemList(dish);
                if ((selectModel != null) && (selectModel.id == employee.id))
                {
                    item.FillColor = System.Drawing.Color.FromArgb(255, 248, 229);
                    lblMobile.Text = selectModel.mobile;
                    lblName.Text = selectModel.name;
                }
                else
                    item.FillColor = System.Drawing.Color.White;
                item.Tag = employee.id.ToString();
                item.TextName = employee.name;
                item.Name = GetFoodItemName(employee.id);
                item.Margin = new System.Windows.Forms.Padding(3);
                item.Click += Item_Click;
                item.SetEnabled();
                fly_datalist.Add(item);
            }
            this.fly_datalist.ResumeLayout();// 添加很多时挂起会更快（不触发界面更改重新绘图事件）
            fly_datalist.Visible = true;
            fly_datalist.Refresh();
        }

        private void Item_Click(object sender, System.EventArgs e)
        {
            UIDishItemList item = (UIDishItemList)sender;
            selectModel = employeeList.Find(p => p.id == CommFunc.ConvertToBigInt(item.Tag));
            if (selectModel != null)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            //AddDataRow(CommFunc.ConvertToBigInt(item.Tag), 1);
        }

        private void numKeybord_OKButtonClick(object sender, string text, int intvalue, double doublevalue)
        {
            SetOkAndClose();
        }

        private void SetOkAndClose()
        {
            if (selectModel == null)
            {
                UIMessageTip.ShowWarning(txtValue, "请选择一个营业员");
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void txtValue_TextChanged(object sender, EventArgs e)
        {
            string textStr = txtValue.Text;
            lblMobile.Text = "";
            lblName.Text = "";
            selectModel = employeeList.Find(p => p.mobile.Contains(textStr) || p.name.Contains(textStr) || p.wechatNumber.Contains(textStr));
            foreach (var employee in employeeList)
            {
                var mControl = fly_datalist.Get(GetFoodItemName(employee.id));
                var item = (UIDishItemList)mControl;
                if (item != null)
                {                    
                    if ((selectModel != null) && (selectModel.id == employee.id))
                    {
                        item.FillColor = System.Drawing.Color.FromArgb(255, 248, 229);
                        lblMobile.Text = selectModel.mobile;
                        lblName.Text = selectModel.name;
                    } 
                    else
                        item.FillColor = System.Drawing.Color.White;
                    item.Refresh();
                }
            }
        }

        private void frmEmployeeSelect_Shown(object sender, EventArgs e)
        {
            txtValue.Focus();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            selectModel = null;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
